package com.lx.commonlib.mvp.view.fragment;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.IdRes;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.lx.commonlib.R;
import com.lx.commonlib.mvp.persenter.BasePresenter;
import com.lx.commonlib.mvp.view.IView.IBaseView;
import com.lx.commonlib.mvp.view.activity.BaseActivity;

/**
*  类说明：APP中所有Fragment 的父类  
*  created by liuxiong on 2017/11/14
**/
public abstract class BaseFragment<A extends BaseActivity, P extends BasePresenter> extends
        Fragment implements IBaseView {

    /**
     * 返回堆栈 存储的fragment的bundle信息
     */
    private Bundle mBackData;

    public P mPresenter;

    /**
     * 根布局
     */
    protected ViewGroup mRootView;
    /**
     * 内容布局
     */
    protected FrameLayout mRootLayout;
    /**
     * 布局填充器
     */
    protected LayoutInflater mLayoutInflater;

    boolean isDestory;  //是否已经销毁

    boolean flag=false; //是否为第一次加载

    boolean isFirst=true;

    protected View statusbarTakePlace; //状态栏占位


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle
            savedInstanceState) {
        if(mRootView==null){
            flag=true;
            mRootView= (ViewGroup) inflater.inflate(R.layout.fragment_base, null);
            mLayoutInflater = LayoutInflater.from(getActivity());
            mPresenter= createPresenter();
        }else{
            flag=false;
        }

        return mRootView;
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
//        mRootView= (ViewGroup) view;
        if(!flag){
            return;
        }
        mRootLayout = (FrameLayout) view.findViewById(R.id.root_fragment_base_content);

        statusbarTakePlace=view.findViewById(R.id.v_statusbar_take_place);

        //初始化状态栏，如果从Fragment跳到Fragment
        if(isNeedFitWindow()){
            initStatusBarHeight(R.id.v_statusbar_take_place);
        }
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if(!flag){
            return;
        }
        if(isFirst){
            isFirst=false;
            onCreateFragment(savedInstanceState);
        }

    }

    /**
     * 创建MVP presenter
     */
    protected abstract P createPresenter();


    /**
    * 方法说明:  在本方法中做初始化操作
    * @param  savedInstanceState
    * created by liuxiong on 2017/11/14 19:19
    */
    protected abstract void onCreateFragment(Bundle savedInstanceState);
    
    /**
    * 方法说明:  是否要预留StatusBar的高度
    * created by liuxiong on 2017/11/14 20:51
    */
    public boolean isNeedFitWindow(){
        return false;
    }

    /**
     * 方法说明:  初始化状态栏占位的控件的高度
     *@author liuxiong
     *@time 2017/1/3 14:08
     */
    protected void initStatusBarHeight(int id){
        View cover = findViewById(id);

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){ //4.4以上
            int statusBarHeight = -1;
            //获取status_bar_height资源的ID
            int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
            if (resourceId > 0) {
                //根据资源ID获取响应的尺寸值
                statusBarHeight = getResources().getDimensionPixelSize(resourceId);
            }
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) cover.getLayoutParams();
            layoutParams.height=statusBarHeight;
            cover.setLayoutParams(layoutParams);
            cover.setVisibility(View.VISIBLE);
        }else{
            cover.setVisibility(View.GONE);
        }
    };


    /**
    * 方法说明:  设置内容布局
    * @param  layoutId 布局Id
    * @return 返回父布局
    * created by liuxiong on 2017/11/14 19:21
    */
    protected View setContentView(@LayoutRes int layoutId) {
        mRootLayout.removeAllViews();
        View view = mLayoutInflater.inflate(layoutId, mRootLayout, false);
        mRootLayout.addView(view);
        return view;
    }

    /**
    * 方法说明:  查找子View
    * @param  id 控件的在布局中的Id
    * @return  id对应的view
    * created by liuxiong on 2017/11/14 20:37
    */
    protected <E extends View> E findViewById(@IdRes int id) {
        return (E) mRootView.findViewById(id);
    }

    /**
    * 方法说明:  刷新数据（在某些页面网络连接成功后调用）
    * created by liuxiong on 2017/12/28 16:14
    */
    public void refreshData(){}


    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        isDestory=true;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

    }

    @Override
    public Context getAppContext() {
        return getContext().getApplicationContext();
    }


    /**
     * @return the bundle  返回
     */
    public Bundle getBackData() {
        return mBackData;
    }

    /**
     *  设置返回数据
     * @param bundle
     */
    public void  setBackData(Bundle bundle){
        this.mBackData = mBackData;
    }

    /**
    * 方法说明:  返回true代表消费返回事件
    * created by liuxiong on 2017/12/20 11:21
    */
    public boolean onBackPressed(){
        return false;
    }

    /**
     * 销毁当前的Fragment
     */
    protected void finish() {
        onFinishBefore();
        getActivity().onBackPressed();
    }

    /**
    * 方法说明:  销毁之前需要做的事情
    * created by liuxiong on 2020/1/14 9:58
    */
    protected void onFinishBefore() { }

}
